class Solution {
public:
    string decodeString(string s) {
        stack<int> st1;
        stack<string> st2;
        st2.push(""); // 放入空串防止越界访问
        int i = 0, n = s.size();
        while(i < n)
        {
            if(s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while(s[i] >= '0' && s[i] <= '9')
                    tmp = tmp * 10 + (s[i++] - '0');
                st1.push(tmp);
            }
            else if(s[i] >= 'a' && s[i] <= 'z')
            {
                string tmp = "";
                while(i < n && s[i] >= 'a' && s[i] <= 'z')
                    tmp += s[i++];
                st2.top() += tmp;
            }
            else if(s[i] == '[')
            {
                ++i;
                string tmp = "";
                while(s[i] >= 'a' && s[i] <= 'z')
                    tmp += s[i++];
                st2.push(tmp);
            }
            else // if(s[i] == ']')
            {
                string tmp = "";
                for(int j = 0; j < st1.top(); ++j)
                    tmp += st2.top();
                st2.pop();
                st2.top() += tmp;
                st1.pop();
                ++i;
            }
        }
        return st2.top();
    }
};